package com.sq.partner.manager.dao.impl;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.sq.partner.manager.dao.IGatewayCoreProdPlanDao;
import com.sq.partner.manager.dao.IGatewayTradPlanBaseDao;
import com.sq.partner.manager.dao.IGatewayTradPlanCovDao;
import com.sq.partner.manager.mybatis.model.CoreProdPlan;
import com.sq.partner.manager.mybatis.model.TradPlanBase;
import com.sq.partner.manager.mybatis.model.TradPlanCov;
import com.sq.partner.manager.mybatis.model.TradProdInst;
import com.sq.partner.manager.mybatis.model.TradProdPlan;
import com.sq.partner.manager.util.DBUtil;
import com.sq.partner.manager.util.LogUtil;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.SqlUtil;
@Repository
public class GatewayTradPlanBaseDaoImpl  extends BaseDaoImpl implements IGatewayTradPlanBaseDao {

	private static Logger logger = LogUtil.getLog(GatewayTradPlanBaseDaoImpl.class);

	
	
	@Override
	public TradPlanBase queryTradPlanBase(TradPlanBase tradPlanBase) throws SQLException {
		// TODO Auto-generated method stub
		return (TradPlanBase) this.query("queryTradPlanBaseByMap", tradPlanBase);
	}

	@Override
	public TradPlanBase findMaxTradPlanCodebyPlanCode(TradPlanBase tradplanbase)
			throws SQLException {
		Object obj= this.query("findMaxTradPlanCodebyPlanCode", tradplanbase);
		if(null!=obj){
			return  (TradPlanBase)obj ;
		}
		return  null ;
	}

	@Override
	public void savetradplanbase(TradPlanBase tradplanbase) throws Exception {
		this.add("insertTradPlanBase", tradplanbase);
	}

	@Override
	public Page<TradPlanBase> queryTradPlanBasePage(Page<TradPlanBase> page,
			String tradProdCode) throws SQLException {
		Connection conn = null;
		//查总条数
		PreparedStatement pstmt = null;
		//查分页
		PreparedStatement pstmt1 = null;
		ResultSet rs = null;
		try{
			conn = this.getConnection();
			StringBuffer sqlbuf=new StringBuffer();
			sqlbuf.append("select tradplanbase.TRAD_PLAN_CODE as tradPlanCode,"
					+ "tradplanbase.TRAD_PLAN_DESC as tradPlanDesc,"
					+ "tradplanbase.AMOUNT as amount,"
					+ "tradplanbase.PLAN_CODE as planCode,"
					+ "tradplanbase.INST_COD as instCod,"
					+ "tradplanbase.pool_ratio as poolRatio,"
					+ "planbase.INSNME as insnme,"
					+ "tradprodplan.INS_TYPE as insType, "
					+ "riskalgmap.cal_def_id as calDefId, "
					+ "riskalgmap.cal_id as calId,nvl(planbase.is_health, 'N') as isHealthFlag "
					+ "from T_TRAD_PLAN_BASE tradplanbase,T_TRAD_PROD_PLAN tradprodplan,T_CORE_PLAN_BASE planbase,T_RISK_ALG_MAP riskalgmap "
					+ " where 1=1 and tradprodplan.TRAD_PROD_CODE=? and tradprodplan.TRAD_PLAN_CODE=tradplanbase.TRAD_PLAN_CODE and tradplanbase.PLAN_CODE=planbase.PLAN_CODE and tradplanbase.INST_COD=planbase.INST_CODE and riskalgmap.trad_plan_code=tradplanbase.TRAD_PLAN_CODE"
					+ " order by tradplanbase.trad_plan_code desc");
			String sql="";
			
			
			sql=sqlbuf.toString();
			logger.debug("执行SQL：" + sql);


			pstmt = conn.prepareStatement("select count(*) from ("+sql+")");

			pstmt.setObject(1, tradProdCode);
			rs = pstmt.executeQuery();
			rs.next();
			Long total = rs.getLong(1);
			
			//查出分页
			page.setTotal(total);
			SqlUtil sqlUtil=new	SqlUtil();
			
			
			String pagesql=sqlUtil.buildPageSqlForOracle(sql,page).toString();
			logger.debug("执行SQL："+pagesql);
			pstmt1 = conn.prepareStatement(pagesql);
			
			
			pstmt1.setObject(1, tradProdCode);
			
			rs = pstmt1.executeQuery();
			List lists = DBUtil.transRs2List(rs);
			page.setData(lists);
		}finally{
			close(rs);
			close(pstmt);
			close(pstmt1);
			close(conn);
		}
		return page;
	}

	@Override
	public void deleteTradPlanBase(TradPlanBase tradPlanBase) throws Exception {
		// TODO Auto-generated method stub
		this.delete("delTradPlanBase", tradPlanBase);
	}

	@Override
	public void updateTradPlanBase(TradPlanBase tradplanbase) throws Exception {
		// TODO Auto-generated method stub
		this.update("updateTradPlanBaseNotNull2", tradplanbase);
	}

	@Override
	public TradPlanBase queryTradPlanBaseByCode(String tradPlanCode)
			throws SQLException {
		return (TradPlanBase)this.query("queryTradPlanBaseById", tradPlanCode);
	}
	
	
	@Override
	public void updateTradPlanBaseAmount(TradPlanBase tradplanbase) throws Exception {
		// TODO Auto-generated method stub
		this.update("updateTradPlanBaseNotNull3", tradplanbase);
	}

	@Override
	public List<TradPlanBase> queryTradPlanBaseInfoByPlanNo(String polNo)
			throws SQLException {
		return this.queryList("queryTradPlanBaseInfoByPlanNo", polNo);
	}
	

}
